using System.Security.Claims;

namespace Terra.Core.Security.Models;

/// <summary>
/// 用户身份信息
/// </summary>
public class UserIdentity
{
    /// <summary>
    /// 用户ID
    /// </summary>
    public string UserId { get; set; } = string.Empty;

    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; } = string.Empty;

    /// <summary>
    /// 真实姓名
    /// </summary>
    public string RealName { get; set; } = string.Empty;

    /// <summary>
    /// 电子邮件
    /// </summary>
    public string Email { get; set; } = string.Empty;

    /// <summary>
    /// 角色列表
    /// </summary>
    public List<string> Roles { get; set; } = new List<string>();

    /// <summary>
    /// 权限列表
    /// </summary>
    public List<string> Permissions { get; set; } = new();

    /// <summary>
    /// 声明列表
    /// </summary>
    public Dictionary<string, string> Claims { get; set; } = new Dictionary<string, string>();

    /// <summary>
    /// 获取用户Claims
    /// </summary>
    public List<Claim> GetClaims()
    {
        var claims = new List<Claim>
        {
            new(ClaimTypes.NameIdentifier, UserId),
            new(ClaimTypes.Name, Username),
            new(ClaimTypes.GivenName, RealName),
            new(ClaimTypes.Email, Email)
        };

        // 添加角色声明
        claims.AddRange(Roles.Select(role => new Claim(ClaimTypes.Role, role)));

        // 添加权限声明
        claims.AddRange(Permissions.Select(permission => new Claim("permission", permission)));

        return claims;
    }
}